using FlowerApi.Common;
using FlowerApi.Dtos;
using FlowerApi.Interfaces;
using FlowerApi.Models;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;

namespace FlowerApi.Controllers;

[ApiController]
[Route("api/[controller]")]
public class FlowerController : ControllerBase
{

    private readonly IFlowerRepository<Flower> _FlowerRep;
    public FlowerController(IFlowerRepository<Flower> FlowerRep)
    {
        _FlowerRep = FlowerRep;
    }

    [HttpGet]
    public async Task<IActionResult> Get()
    {
        var list = await _FlowerRep.GetAllFlowerAsync();
        if (list.Count == 0)
        {
            return Ok(ApiResponse<string>.Fail(1004, "暂无数据"));
        }
        return Ok(ApiResponse<List<Flower>>.Success(list));
    }

    [HttpPost]
    public async Task<IActionResult> Post(FlowerDto flowerDto)
    {
        var flower = new Flower { FlowerName = flowerDto.FlowerName, Stock = flowerDto.Stock };
        await _FlowerRep.AddFlowerAsync(flower);
        return Ok(ApiResponse<FlowerDto>.Success(flowerDto));
    }

    [HttpPut("{guid}")]
    public async Task<IActionResult> Put(Guid guid, FlowerDto flowerDto)
    {
        var flower = await _FlowerRep.GetFlowerByIdAsync(guid);
        if (flower == null)
        {
            return Ok(ApiResponse<string>.Fail(1004, "暂无此花,修改失败"));
        }
        await _FlowerRep.EditFlowerAsync(flower);
        return Ok(ApiResponse<FlowerDto>.Success(flowerDto, "修改成功"));
    }

    [HttpDelete("{guid}")]
    public async Task<IActionResult> Delete(Guid guid)
    {
        var flower = await _FlowerRep.GetFlowerByIdAsync(guid);
        if (flower == null)
        {
            return Ok(ApiResponse<string>.Fail(1004, "暂无此花,删除失败"));
        }
        await _FlowerRep.DelFLowerAsync(flower);
        return Ok(ApiResponse<Flower>.Success(flower, "删除成功"));
    }

}